/**
 * 
 * 4512376594  随机丢弃k个数 返回最小的数
 * 贪心算法
 */

 function getMinNumber (srcStr: string, k: number): number {
  let numStack = new Array()
  let lastIndex = 0
  for (let i = 0; i < srcStr.length; i++) {
    let s = srcStr[i]
    if (k == 0) {
      lastIndex = i
      break
    }
    let stackLen = numStack.length
    if (stackLen == 0) {
      numStack.push(s)
    } else {
      if (s < numStack[stackLen-1]) {
        do {
          console.log(numStack.pop())
          k--
        } while (numStack.length > 0 && k > 0 && s < numStack[numStack.length-1])
        
      } 
      numStack.push(s)
    }
  } 

  if (k > 0) {
    numStack.splice(-k)
  }

  let numStr = numStack.join("")

  if (lastIndex < srcStr.length - 1) {
    numStr += srcStr.slice(lastIndex)
  }

  return +numStr
 }

 console.log(getMinNumber("4512376594", 4))